package com.chhh.cranelease.mapper;

import com.chhh.cranelease.entity.CarLog;
import com.chhh.cranelease.entity.Parts;
import com.chhh.cranelease.entity.Repair;
import com.chhh.cranelease.model.CarLogModel;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface BusinessManageMapper {

    @Select("<script>"
            +"select cl_id,cl_num,cl_name,cl_outtime,cl_company,cl_days,cl_price,clstate_name,cl_oilmoney from car_log left join cl_state on cl_state=clstate_id" +
            "<where>" +
            "<if test='cl_num!=null and cl_num!=\"\"'>and cl_num like CONCAT('%',#{cl_num},'%')</if>" +
            "<if test='cl_name!=null and cl_name!=\"\"'>and cl_name like CONCAT('%',#{cl_name},'%')</if>" +
            "<if test='cl_outtime!=null and cl_outtime!=\"\"'>and cl_outtime like CONCAT('%',#{cl_outtime},'%')</if>" +
            "<if test='cl_days!=null and cl_days!=\"\"'>and cl_days like CONCAT('%',#{cl_days},'%')</if>" +
            "<if test='cl_company!=null and cl_company!=\"\"'>and cl_company like CONCAT('%',#{cl_company},'%')</if>" +
            "</where>" +
            "</script>")
    List<CarLogModel> selectAllUseCar(@Param("cl_num") String cl_num, @Param("cl_name") String cl_name, @Param("cl_outtime") String cl_outtime, @Param("cl_days") String cl_days, @Param("cl_company") String cl_company);

    @Insert("insert into car_log (cl_num,cl_name,cl_outtime,cl_company,cl_days,cl_price,cl_state,cl_oilmoney)" +
            "values(#{cl_num},#{cl_name},#{cl_outtime},#{cl_company},#{cl_days},#{cl_price},#{cl_state},#{cl_oilmoney})")
    Integer insertCarLog(CarLog carLog);

    @Update("update car_log set cl_num=#{cl_num},cl_name=#{cl_name},cl_outtime=#{cl_outtime},cl_company=#{cl_company},cl_days=#{cl_days},cl_price=#{cl_price},cl_state=#{cl_state},cl_oilmoney=#{cl_oilmoney} where cl_id = #{cl_id}")
    Integer updateCarLog(CarLog carLog);


    // 配件管理
    @Select("<script>"+
            "select p_id,p_num,p_date,p_des from parts " +
            "<where>" +
            "<if test='p_date!=null and p_date!=\"\"'>and p_date like CONCAT('%',#{p_date},'%')</if>" +
            "<if test='p_num!=null and p_num!=\"\"'>and p_num like CONCAT('%',#{p_num},'%')</if>" +
            "<if test='p_des!=null and p_des!=\"\"'>and p_des like CONCAT('%',#{p_des},'%')</if>" +
            "</where>" +
            "</script>")
    List<Parts> selectAllParts(@Param("p_date") String p_date, @Param("p_des") String p_des,@Param("p_num") String p_num);

    @Insert("insert into parts (p_num,p_date,p_des) values(#{p_num},#{p_date},#{p_des})")
    Integer insertParts(Parts parts);

    @Update("update parts set p_num=#{p_num},p_date=#{p_date},p_des=#{p_des} where p_id = #{p_id}")
    Integer updateParts(Parts parts);

    @Delete("<script>" +
            "delete from parts where p_id in" +
            "<foreach collection=\"array\" item=\"id\" separator=\",\" open=\"(\" close=\")\">" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    Integer deleteParts(Integer[] p_id);

    // 当维修好以后修改汽车状态为工作
    @Update("update car_log set cl_state=#{r_state} where cl_num=#{r_num}")
    void updateCarLogState(Repair repair);
}
